home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
smaltalk
/
manchest.lha
/
MANCHESTER
/
manchester
/
2.2
/
Form-shrinkBy.st
< prev
next >
Wrap
Text File
|
1993-07-24
|
5KB
|
154 lines
" NAME Form-shrinkBy
AUTHOR Dr Who
FUNCTION see Form-features
ST-VERSIONS
PREREQUISITES
CONFLICTS
DISTRIBUTION world
VERSION 1.1
DATE 22 Jan 1989"
'From Smalltalk-80, version 2, of April 1, 1983 on 3 January 1987 at 7:42:45 pm'!
!Form methodsFor: 'image manipulation'!
shrinkBy2
"Answers with a copy of the receiver, scaled down by 2 in each direction.
The 4 bits corresponding to each bit in the new form are summed,
returning a value in the range 0 to 4. The resulting
bit is set to black (1) if 2 or more bits are set, otherwise it is set
to white (0). For performance reasons, the arithmetic is done with
BitBlt operations. This special method uses a better algorithm than the
general shrinkBy: method."
"To see the improvement this method has over the general one,
evaluate the following expressions."
"
| tempForm |
Display white.
tempForm _
Form readFrom: '/usr/r3/appl/PS/goodies/Manchester_goodies/Astronaut.form'.
tempForm displayAt: Display boundingBox topCenter.
tempForm shrinkBy2 displayAt: Display boundingBox topLeft.
(tempForm shrinkBy: 2@2) displayAt: Display boundingBox leftCenter.
Sensor waitButton.
ScheduledControllers restore.
"
| nbr1 nbr2 nbr4 carry2 carry4 all delta
wideForm shrunkenForm saveOffset |
nbr1 _ Form extent: self extent.
nbr2 _ Form extent: self extent.
nbr4 _ Form extent: self extent.
carry2 _ Form extent: self extent.
carry4 _ Form extent: self extent.
all _ self boundingBox.
1 to: 4 do:
[:i |
delta _ ((#(0 1 1 0) at: i) @ (#(0 0 1 1) at: i)).
carry2 copy: all from: 0@0 in: nbr1 rule: 3.
carry2 copy: all from: delta in: self rule: 1. "AND for carry into 2"
nbr1 copy: all from: delta in: self rule: 6. "XOR for sum 1"
carry4 copy: all from: 0@0 in: nbr2 rule: 3.
carry4 copy: all from: 0@0 in: carry2 rule: 1. "AND for carry into 4"
nbr2 copy: all from: 0@0 in: carry2 rule: 6. "XOR for sum 2"
nbr4 copy: all from: 0@0 in: carry4 rule: 6 "XOR for sum 4 (ignore carry)"
].
nbr4 copy: all from: 0@0 in: nbr2 rule: 7. "OR top 2 bits."
saveOffset _ self offset.
self offset: 0 @ 0.
wideForm _ Form new extent: self width @ (self height // 2).
0 to: wideForm height - 1 do:
[:index |
wideForm copy: (0 @ index extent: wideForm width @ 1)
from: 0 @ (index * 2)
in: nbr4
rule: Form over].
shrunkenForm _ Form new extent: (self width // 2) @ wideForm height.
0 to: shrunkenForm width - 1 do:
[:index |
shrunkenForm
copy: (index @ 0 extent: 1 @ wideForm height)
from: (index * 2) @ 0
in: wideForm
rule: Form over].
self offset: saveOffset.
shrunkenForm offset: (offset // 2).
^shrunkenForm! !'From Smalltalk-80, version 2, of April 1, 1983 on 3 January 1987 at 7:42:55 pm'!
!Form methodsFor: 'image manipulation'!
shrinkBy3
"Answers with a copy of the receiver, scaled down by 3 in each direction.
The 4 bits corresponding to each bit in the new form are summed,
returning a value in the range 0 to 9. The resulting
bit is set to black (1) if 5 or more bits are set, otherwise it is set
to white (0). For performance reasons, the arithmetic is done with
BitBlt operations. This special method uses a better algorithm than the
general shrinkBy: method."
"Form fromUser shrinkBy3 display."
| nbr1 nbr2 nbr4 nbr8 br2 br4 br8
carry2 carry4 carry8 all delta wideForm shrunkenForm saveOffset |
nbr1 _ Form extent: self extent.
nbr2 _ Form extent: self extent.
nbr4 _ Form extent: self extent.
nbr8 _ Form extent: self extent.
carry2 _ Form extent: self extent.
carry4 _ Form extent: self extent.
carry8 _ Form extent: self extent.
all _ self boundingBox.
1 to: 9 do:
[:i |
"delta is the offset of the eight neighboring cells, plus self."
delta _
((#(-1 0 1 1 1 0 -1 -1 0) at: i) @ (#(-1 -1 -1 0 1 1 1 0 0) at: i)).
carry2 copy: all from: 0@0 in: nbr1 rule: 3.
carry2 copy: all from: delta in: self rule: 1. "AND for carry into 2"
nbr1 copy: all from: delta in: self rule: 6. "XOR for sum 1"
carry4 copy: all from: 0@0 in: nbr2 rule: 3.
carry4 copy: all from: 0@0 in: carry2 rule: 1. "AND for carry into 4"
nbr2 copy: all from: 0@0 in: carry2 rule: 6. "XOR for sum 2"
carry8 copy: all from: 0@0 in: nbr4 rule: 3.
carry8 copy: all from: 0@0 in: carry4 rule: 1. "AND for carry into 8"
nbr4 copy: all from: 0@0 in: carry4 rule: 6. "XOR for sum 4"
nbr8 copy: all from: 0@0 in: carry8 rule: 6]. "XOR for sum 8 (ignore carry)"
nbr2 copy: all from: 0@0 in: nbr1 rule: 7.
nbr4 copy: all from: 0@0 in: nbr2 rule: 1.
nbr8 copy: all from: 0@0 in: nbr4 rule: 7.
"Logic is nbr8 OR (nbr4 AND (nbr2 OR nbr1))."
saveOffset _ self offset.
self offset: 0 @ 0.
wideForm _ Form new extent: self width @ (self height // 3).
0 to: wideForm height - 1 do:
[:index |
wideForm copy: (0 @ index extent: wideForm width @ 1)
from: 0 @ (index * 3)
in: nbr8
rule: Form over].
shrunkenForm _ Form new extent: (self width // 3) @ wideForm height.
0 to: shrunkenForm width - 1 do:
[:index |
shrunkenForm
copy: (index @ 0 extent: 1 @ wideForm height)
from: (index * 3) @ 0
in: wideForm
rule: Form over].
self offset: saveOffset.
shrunkenForm offset: (offset // 3).
^shrunkenForm! !